// var fs = require('fs.extra');
var path = require('path');
var fs = require('fs');
var root = path.join(__dirname, '..', 'src');
var dist = path.join(__dirname ,'..','..', 'adf');
var jsfiles = require('./config/js_config.js').files;
var cssfiles = require('./config/css_config.js').files;
var jsdistFileName = path.join(dist, 'vue_adf_core.js');
var cssdistFileName = path.join(dist, 'vue_adf_core.css');
var jsdistFile = fs.createWriteStream(jsdistFileName);
var cssdistFile = fs.createWriteStream(cssdistFileName);

module.exports = function(callback){
	function reduce(files, distFile,callback){
		function next(i){
			if(i >= files.length){
				return callback();
			} 
			var file = files[i];
			console.log('combine file: '+path.join(root, file));
			var src = fs.createReadStream(path.join(root, file));
			src.pipe(distFile, { end: false });
			src.on('end', next.bind(null, ++i));
		}
		next.bind(null,0)();
	};
	reduce(jsfiles,jsdistFile, function(){
		reduce(cssfiles, cssdistFile, function(){
			console.log('\x1b[32m%s\x1b[0m ', 'bcp core build completed.');
			if(callback){
				callback();
			}
		});
	});
}
